package game;
import java.awt.Color;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;

import networking.SMap;

public class Map
{
	private static int[][] m;
	private static int width;
	private static int height;
	private static ArrayList<Point> targets = new ArrayList<Point>();
	private static Color location;

	public static int getHeight()
	{
		return height;
	}

	public static int getWidth()
	{
		return width;
	}

	public static void setMap(SMap map)
	{
		targets.clear();
		
		m = map.getMap();
		
		height = map.getHeight();
		width = map.getWidth();
		for(int i=0; i<width; i++)
		{
			for(int j=0; j<height; j++)
			{
				location = new Color(m[i][j]);
				//Green is when green is greater than 20, and blue and red are 0
				if(location.getRed() == 0 && location.getBlue() == 0 && location.getGreen() > 20)
				{
					targets.add(new Point(j, i));
				}
			}
		}
		
	}
	
	public static void setMap(String map)
	{
		targets.clear();
		try
		{
			File inputFile = new File(map);
			BufferedImage b = ImageIO.read(inputFile);
			height = b.getHeight();
			width = b.getWidth();
			m = new int[width][height];
			for(int i=0; i<height; i++)
			{
				for(int j=0; j<width; j++)
				{
					location = new Color(b.getRGB(j,i));
					m[i][j] = b.getRGB(j, i);
					//Green is when green is greater than 20, and blue and red are 0
					if(location.getRed() == 0 && location.getBlue() == 0 && location.getGreen() > 20)
					{
						targets.add(new Point(i, j));
					}
				}
			}
		}
		catch(IOException e)
		{
			System.out.println("File not found: "+map);
		}
	}

	public static boolean isSolid(int x, int y)
	{
		location = new Color(m[y/10][x/10]);
		//Blue is when blue is greater than 20, and red and green are 0.
		if(location.getBlue() > 20 && location.getRed() == 0 && location.getGreen() == 0)
		{
			return true;
		}
		return false;
	}

	public static int checkSpeed(int x, int y)
	{
		location = new Color(m[y/10][x/10]);
		//Red is when red is greater than 20, and blue and green are 0
		if(location.getRed() > 20 && location.getBlue() == 0 && location.getGreen() == 0)
		{
			return 1;
		}
		//Gray is when red and blue and green are equal
		if(location.getRed() == location.getBlue() && location.getBlue() == location.getGreen() && location.getGreen() > 0)
		{
			return 4;
		}
		return 2;
	}
	public static boolean reverseControl(int x, int y)
	{
		location = new Color(m[y/10][x/10]);
		//Cyan is when green and blue are equal, and red is 0.  Green and blue should not be allowed to 0.
		if(location.getRed() == 0 && location.getBlue() == location.getGreen() && location.getGreen() > 0)
		{
			return true;
		}
		return false;
	}

	public static int getValue(int x, int y)
	{
		return m[y][x];
	}
	public static boolean isTarget(int x, int y)
	{
		location = new Color(m[y/10][x/10]);
		//Green is when green is greater than 20, and blue and red are 0
		if(location.getRed() == 0 && location.getBlue() == 0 && location.getGreen() > 20)
		{
			return true;
		}
		return false;
	}
	public static boolean isHazard(int x, int y)
	{
		location = new Color(m[y/10][x/10]);
		//For brown, red is double green, and blue is 0.
		if(location.getBlue() == 0 && (location.getRed() / 2) == location.getGreen() && location.getGreen() > 0)
		{
			return true;
		}
		return false;
	}
	public static int targetCount()
	{
		return targets.size();
	}
	
	public static ArrayList<Point> getTargets()
	{
		return targets;
	}
}
